<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 向数组中追加 K 个整数 -->
    <script>
      var minimalKSum = function (nums, k) {
        let set = new Set(nums)
        let sum = ((1 + k) * k) / 2
        let next = k + 1
        for (let n of set) {
          if (n <= k) {
            while (set.has(next)) next++
            ;(sum += next - n), next++
          }
        }
        return sum
      }
      console.log(minimalKSum([1, 4, 25, 10, 25], 2))
    </script>
  </body>
</html>
